<?php
/**
 * User:ayou chow
 * Date: 2020-01-17
 * Time: 09:44
 */

namespace app\admin\controller;


use app\common\model\NoticeModel;
use think\facade\Request;
use think\Validate;

class PushController extends BaseController {

    public function index() {
        return $this->fetch();
    }

    public function getlist() {
        $id        = Request::param("id");
        $user_id   = Request::param("user_id");
        $where     = [];
        $wherelike = '';
        if ($id) {
            $where['id'] = $id;
        }
        if ($user_id) {
            $wherelike = "user_id like '%{$user_id}%' or user_id = 0";
        }
        $page  = Request::param("page");
        $limit = Request::param("limit");
        $msgs  = NoticeModel::where($where)->where($wherelike)->limit(($page - 1) * $limit, $limit)->order('create_time', 'desc')->select();
        if (count($msgs) > 0) {
            $count = NoticeModel::where($where)->where($wherelike)->count();
            return json(["code" => 0, "msg" => "", "data" => $msgs, "count" => $count]);
        }
        return self::bulidFail("未查询到相关数据");
    }

    public function add() {
        return $this->fetch();
    }

    public function add_post() {
        $title     = Request::param('title');
        $content   = Request::param('content');
        $user_id   = Request::param('user_id');
        $image_url = Request::param('image_url', '');
        $link      = Request::param('link', '');

        $validate = new Validate([
            'title'   => 'require|max:50',
            'content' => 'require|max:200',
        ], [
            "title.require"   => "请输入标题",
            "title.max"       => "标题最多50字",
            "content.require" => "请输入内容",
            "content.max"     => "内容最多200字",
        ]);

        if (!$validate->check(Request::param())) {
            return self::bulidFail($validate->getError());
        }

        // 组合接收用户数组 0为所有用户
        $uid_arr = [0];
        if (!empty($user_id)) {
            $uid_arr = explode(',', $user_id);
            $uid_arr = array_unique($uid_arr);
            $uid_arr = array_filter($uid_arr, function ($v, $k) {
                return !empty($v) && is_numeric($v) && intval($v) > 0;
            }, ARRAY_FILTER_USE_BOTH);
        }


        if (empty($uid_arr)) {
            return self::bulidFail('请正确填写接收用户');
        }

        // 组合接收用户批量入库
        $data = array();
        foreach ($uid_arr as $val) {
            $data[] = [
                'user_id'     => $val,
                'title'       => $title,
                'content'     => $content,
                'image_url'   => $image_url,
                'link'        => $link,
                'create_time' => nowFormat()
            ];
        }

        $notice_model = new NoticeModel;
        $res          = $notice_model->isUpdate(false)->saveAll($data);
        if (count($res) <= 0) {
            return self::bulidFail();
        }

        // 记录日志
        $log_content = "发布消息，标题：{$title}，接收用户:";
        $log_content .= (!empty($user_id) ? implode(',', array_unique(array_filter($uid_arr))) : '全员');
        parent::addAdminLog($log_content);
        return self::bulidSuccess();
    }
}